package com.nepu.spark.realtime.util

import com.nepu.spark.realtime.constant.RedisConstant
import redis.clients.jedis.{Jedis, JedisPool, JedisPoolConfig}

/**
  * Redis的工具类
  *
  * @author chenshuaijun
  * @create 2022-10-30 14:05
  */
object MyRedisUtils {

  private var jedisPool: JedisPool = null

  def getJedisClient(): Jedis = {
    if (jedisPool == null) {
      var host: String = MyConfPropertiesUtils(RedisConstant.REDIS_HOST)
      var port: String = MyConfPropertiesUtils(RedisConstant.REDIS_PORT)
      val jedisPoolConfig = new JedisPoolConfig()
      jedisPoolConfig.setMaxTotal(100) //最大连接数
      jedisPoolConfig.setMaxIdle(20) //最大空闲
      jedisPoolConfig.setMinIdle(20) //最小空闲
      jedisPoolConfig.setBlockWhenExhausted(true) //忙碌时是否等待
      jedisPoolConfig.setMaxWaitMillis(5000) //忙碌时等待时长 毫秒
      jedisPoolConfig.setTestOnBorrow(true) //每次获得连接的进行测试
      jedisPool = new JedisPool(jedisPoolConfig, host, port.toInt)
    }
    jedisPool.getResource
  }

}
